package com.fjh.springboot_01.service.impl;

import com.fjh.springboot_01.mapper.LessonLikeMapper;

import com.fjh.springboot_01.pojo.lesson.LessonLike;
import com.fjh.springboot_01.service.LessonLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class LessonLikeServiceImpl implements LessonLikeService {
    @Autowired
    private LessonLikeMapper lessonLikeMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void like(Integer lessonId, Integer userId) {
        // 检查是否已经点赞
        if (isLiked(lessonId, userId)) {
            return;
        }

        // 添加点赞记录
        LessonLike lessonLike = new LessonLike();
        lessonLike.setLessonId(lessonId);
        lessonLike.setUserId(userId);
        lessonLike.setCreateTime(LocalDateTime.now());
        lessonLikeMapper.add(lessonLike);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void unlike(Integer lessonId, Integer userId) {
        lessonLikeMapper.delete(lessonId, userId);
    }

    @Override
    public boolean isLiked(Integer lessonId, Integer userId) {
        return lessonLikeMapper.exists(lessonId, userId) > 0;
    }

    @Override
    public Integer getLikeCount(Integer lessonId) {
        return lessonLikeMapper.count(lessonId);
    }
} 